<template>
  <div class="addDetail">
    <el-form ref="form" :model="form" label-width="80px" style="width:500px;">
      <el-form-item label="商品图片">
        <el-input v-model="form.pic_url"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="商品价格">
        <el-input v-model="form.price"></el-input>
      </el-form-item>
      <el-form-item label="品牌名称">
        <el-input v-model="form.brand"></el-input>
      </el-form-item>
      <el-form-item label="商品分类">
        
        <el-cascader :options="options" placeholder="请选择" style="width: 100%;" clearable v-model="value"
          :props="{ value: 'id', label: 'name', children: 'items' }" @change="handleChange">
        </el-cascader>
      </el-form-item>
      <el-form-item label="商品简介">
        <el-input type="textarea" v-model="form.detail_info"></el-input>
      </el-form-item>
      <el-form-item label="商品库存">
        <el-input v-model="form.stock"></el-input>
      </el-form-item>
      <el-form-item label="商品标签">
        <el-input v-model="form.tags"></el-input>
      </el-form-item>
      <el-form-item label="优惠券id">
        <el-input v-model="form.coupon_id"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addGoods, getCategoryInfo, editGoods, getDetail } from '@/api/goods'


export default {
  data() {
    return {
      options: [],
      value: [],
      form: {

      },
      level1_category_id: 0,
      level1_category_id: 0,
      level1_category_id: 0,

    }
  },

  created() {
    //判断进入的是添加还是编辑
    if (this.$route.query.id) {
      let id = this.$route.query.id
      getDetail({ id }).then((res) => {
        //console.log(res)
        this.form = { ...res.data }
        this.value = [
          res.data.level1_category_id,
          res.data.level2_category_id,
          res.data.level3_category_id
        ]

      })
    }
    this.getCategoryList()
  },

  
  methods: {
    //获取级联列表
    getCategoryList() {
      getCategoryInfo({ page: 1, limit: 100 }).then((res) => {
        // console.log(123,res)
        this.options = res.data.list
        // console.log(this.value)
      })
    },
    //添加
    add() {
      addGoods({
        ...this.form,
        level1_category_id: this.value[0],
        level2_category_id: this.value[1],
        level3_category_id: this.value[2],
      }).then((res) => {
        if (res.code === 1) {
          this.$message.success('添加成功')
          // this.$bus.$emit('getGoodsList')
        }
      })
    },
    //编辑
    edit() {
      editGoods({
        ...this.form,
        level1_category_id: this.value[0],
        level2_category_id: this.value[1],
        level3_category_id: this.value[2],
      }).then((res) => {
        // console.log(123, res)
        if (res.code == 1) {
          this.$message.success('编辑成功')
         //  this.$bus.$emit('getGoodsList')
        }
      })
    },
    //判断添加还是编辑
    onSubmit() {
      if (this.$route.query.id) {
        this.edit(this.$route.query.id) 
      } else {
        this.add()  
      }
       this.cancel()
    },
    cancel() {
      this.form = {}
      this.value = []
      this.$router.push('/productManager/productList')
    },
    handleChange() {

    }
  },
}

</script>

<style scoped>
.addDetail {
  padding: 20px;
  box-sizing: border-box;
  padding-left: 40px;
}
</style>